<template>
  <div class="center">
    <div class="flex searchBox flex_bet felx_align">
      <div class="flex felx_align">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-input v-model="listQuery.sn" placeholder="请输入设备SN" class="input-with-select" size="mini" style="margin-left:10px">
          <el-button slot="append" type="primary" round @click="getfindSnPackage">搜索</el-button>
        </el-input>
      </div>
      <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
    </div>
    <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="username" label="归属" min-width="110" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="sn" label="SN码" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="app_name" label="应用名称" min-width="140" align="center"></el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="app_package" label="应用包名" min-width="160" align="center"></el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="app_version_name" label="版本号" min-width="80" align="center"></el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="install_time" label="安装时间" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.install_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="80" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="getuninstallPackage(scope.row)"></el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { findSnPackage, uninstallPackage } from "@/api/supervise";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable },
  data() {
    return {
      searchSn: '',
      checkboxVal: ["归属", "SN码", "应用名称", "应用包名", "版本号", "安装时间"],
      key: 1,
      colData: [], //动态控制table
      brandTable: [],
      select: "", //搜索条件
      searchTable: "",
      total: 0, //总数量
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), sn: undefined },
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
  },
  methods: {
    //卸载
    getuninstallPackage(row) {
      this.$confirm('确定卸载此应用?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let qs = { userId: localStorage.getItem("ID"), sn: row.sn, package_name: row.app_package }
        console.log('参数', qs)
        uninstallPackage(qs).then(res => {
          this.$message({ type: 'success', message: '删除成功!' })
          setTimeout(() => {
            this.getfindSnPackage()
          }, 2000);
        })
      })
    },
    getfindSnPackage() {
      findSnPackage(this.listQuery).then(res => {
        this.brandTable = res.data
        this.total = res.total
        console.log('---------', res)
      }).catch(err => {
        this.brandTable = []
        this.total = 0
        console.log('---------', err)
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getfindSnPackage();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getfindSnPackage();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), sn: undefined }
      this.getfindSnPackage()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>

<style scoped>
.searchBox {
  margin-bottom: 10px;
}
</style>